package com.example.secondhandsysapi.Mapper;

import com.example.secondhandsysapi.Entity.OrderInfo;
import com.example.secondhandsysapi.Vo.AwaitingShipment;
import com.example.secondhandsysapi.Vo.Money;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

@Mapper
public interface OrderInfoMapper {
    @Select("select * from order_info")
    List<OrderInfo> getOrderInfo();


    @Select("select * from order_info where order_createtime like '%${date}%'")
    List<OrderInfo> getOrderInfoByDate(@Param("date") String date);

    @Select("select * from order_info where order_state like '%${state}%'")
    List<OrderInfo> getOrderInfoByState(@Param("state") String state);

    @Select("select * from order_info where order_createtime like '%${date}%' and order_state like '%${state}%'")
    List<OrderInfo> getOrderInfoByDateAndState(@Param("date") String date,@Param("state") String state);

    //获取待发货订单
    @Select("SELECT *  \n" +
            "FROM order_info   \n" +
            "JOIN order_details  ON order_info.order_id = order_details.order_id  \n" +
            "JOIN goods_info  ON order_details.goods_id = goods_info.goods_id  \n" +
            "WHERE order_info.order_state = '待发货'")
    List<AwaitingShipment> getAwaitingShipmentOrderInfoByState( );

    //获取待付款订单
    @Select("SELECT *  \n" +
            "FROM order_info   \n" +
            "JOIN order_details  ON order_info.order_id = order_details.order_id  \n" +
            "JOIN goods_info  ON order_details.goods_id = goods_info.goods_id  \n" +
            "WHERE order_info.order_state = '待付款'")
    List<AwaitingShipment> getAwaitingPaymentOrderInfoByState( );


    //获取在途中订单
    @Select("SELECT *  \n" +
            "FROM order_info   \n" +
            "JOIN order_details  ON order_info.order_id = order_details.order_id  \n" +
            "JOIN goods_info  ON order_details.goods_id = goods_info.goods_id  \n" +
            "WHERE order_info.order_state = '在途中'")
    List<AwaitingShipment> getRoadingShipmentOrderInfoByState( );

    //获取已完成订单
    @Select("SELECT * \n" +
            "FROM order_info   \n" +
            "JOIN order_details  ON order_info.order_id = order_details.order_id  \n" +
            "JOIN goods_info  ON order_details.goods_id = goods_info.goods_id  \n" +
            "WHERE order_info.order_state = '已完成'")
    List<AwaitingShipment> getFinishingShipmentOrderInfoByState( );

    //获取全部订单
    @Select("SELECT *  \n" +
            "FROM order_info   \n" +
            "JOIN order_details  ON order_info.order_id = order_details.order_id  \n" +
            "JOIN goods_info  ON order_details.goods_id = goods_info.goods_id  \n" +
            "WHERE order_info.order_state = '已完成' or order_info.order_state = '已取消'")
    List<AwaitingShipment> getAllOrderInfoByState( );

    //修改订单地址
    @Update("UPDATE order_info  \n" +
            "SET delivery_address = #{delivery_address}  \n" +
            "WHERE order_id =#{order_id}")
    Integer updateDeliveryAddress(@Param("delivery_address") String delivery_address,@Param("order_id") Integer order_id);

//修改商品状态
    @Update("UPDATE order_info  \n" +
            "SET order_state = #{order_state}  \n" +
            "WHERE order_id =#{order_id}")
    Integer updateOrderState(@Param("order_state") String order_state,@Param("order_id") Integer order_id);


    //删除待支付order
    @Update("UPDATE order_info  \n" +
            "SET order_state ='已取消' \n" +
            "WHERE order_id =#{order_id}")
    Integer deleteOrderInfo(@Param("order_id") Integer order_id);

    //按照日期获取商品交易额
    @Select("SELECT SUM(total_price) as total_amount\n" +
            "FROM order_info\n" +
            "WHERE DATE(order_finishtime) like '%${date}%' and order_state = '已完成' ")
    Double getMoneyByDay(@Param("date") Date date);
}
